package com.example.origin.demo.utils;

import jdk.nashorn.internal.runtime.logging.Logger;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.MemoryUsage;
import java.lang.management.ThreadMXBean;

@Slf4j
@UtilityClass
public class VmUtil {

    public void logBase() {
//        System.gc();
        log.info("<<<<<<<<<<<<<<<<<<<<<<<<< Vm Base Info >>>>>>>>>>>>>>>>>>>>>>>>>");
        for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
            MemoryUsage usage = pool.getUsage();
            String string = toString(usage.getInit(), usage.getUsed(), usage.getCommitted(), usage.getMax());
            log.info(String.format("%23.23s - %s",pool.getName(),string));
        }

    }
    public String toString(long init,long used,long committed,long max) {
        StringBuffer buf = new StringBuffer();
        buf.append("init = " +  (init >> 10)/1024 + "M ");
        buf.append("used = " +  (used >> 10)/1024 + "M ");
        buf.append("committed = " +
                (committed >> 10)/1024 + "M " );
        buf.append("max = " +  (max >> 10)/1024 + "M");
        return buf.toString();
    }
}
